package com.example.util;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import com.example.RouterHandle.GoEvent;
import com.example.common.RequestEnum;
import com.example.myapplication.*;
import com.example.myapplication.fragmentView.FragmentActivity;
import com.example.myapplication.ip.AddressView;
import com.example.myapplication.preview.CameraActivity;
import com.example.myapplication.progress.ProgressActivity;
import com.example.myapplication.textview.TextViewActitvity;
import com.example.myapplication.toast.ToastActivity;
import com.example.myapplication.view.ListViewActivity;
import com.example.myapplication.view.RecyclerViewActivity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class RegisterEventUtil {

    /**
     * 给main视图中所有Button注册跳转事件
     * @param activity mainActivity
     */
    public static void registerMainButtonAndHandle(AppCompatActivity activity) {
        GoEvent.registerFunction(R.id.horizontalButton, context -> context.startActivity(new Intent(context, HorizontalLayoutActivity.class)));
        GoEvent.registerFunction(R.id.verticalButton, context -> context.startActivity(new Intent(context, VerticalLayoutActivity.class)));
        GoEvent.registerFunction(R.id.tableButton, context -> context.startActivity(new Intent(context, TableLayoutActivity.class)));
        GoEvent.registerFunction(R.id.gridButton, context -> context.startActivity(new Intent(context, GridLayouttActivity.class)));
        GoEvent.registerFunction(R.id.frameButton, context -> context.startActivity(new Intent(context, FrameLayoutActivity.class)));
        GoEvent.registerFunction(R.id.relativeButton, context -> context.startActivity(new Intent(context, RelativeLayoutActivity.class)));
        GoEvent.registerFunction(R.id.fragmentButton, context -> context.startActivity(new Intent(context, FragmentActivity.class)));
        GoEvent.registerFunction(R.id.RecyclerButton, context -> context.startActivity(new Intent(context, RecyclerViewActivity.class)));
        GoEvent.registerFunction(R.id.toastButton, context -> context.startActivity(new Intent(context, ToastActivity.class)));
        GoEvent.registerFunction(R.id.progressButton, context -> context.startActivity(new Intent(context, ProgressActivity.class)));
        GoEvent.registerFunction(R.id.captrueButton, context -> context.startActivity(new Intent(context, CameraActivity.class)));
        GoEvent.registerFunction(R.id.ListViewButton, context -> context.startActivity(new Intent(context, ListViewActivity.class)));
        GoEvent.registerFunction(R.id.addrButton, context -> context.startActivity(new Intent(context, AddressView.class)));
        GoEvent.registerFunction(R.id.arpButton, context -> {

                new Thread(() -> {
                    try {
                        Process process = Runtime.getRuntime().exec("arp -a");
                        InputStream inputStream = process.getInputStream();

                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();


        });
        GoEvent.registerFunction(R.id.vidieButton, context -> {
            AppCompatActivity con = (AppCompatActivity) context;
            File storageDir = con.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File photoFile = null;
            try {
                photoFile = File.createTempFile(
                        "1243",  /* prefix */
                        ".mp4",         /* suffix */
                        storageDir      /* directory */
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
            Uri photoURI = FileProvider.getUriForFile(con,
                    "com.example.myapplication.FileProvider",
                    photoFile);


            Intent intent = new Intent(context, CameraActivity.class);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            intent.putExtra("v", "ok");

            context.startActivity(intent);
        });
        GoEvent.registerFunction(R.id.textviewButton, context -> context.startActivity(new Intent(context, TextViewActitvity.class)));
        GoEvent.registerFunction(R.id.recordButton, context -> {

//            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Intent intent1 = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            AppCompatActivity con = (AppCompatActivity) context;
            File storageDir = con.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File photoFile = null;
            try {
                photoFile = File.createTempFile(
                        "123",  /* prefix */
                        ".MP4",         /* suffix */
                        storageDir      /* directory */
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
            Uri photoURI = FileProvider.getUriForFile(con,
                    "com.example.myapplication.FileProvider",
                    photoFile);

            intent1.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            intent1.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
            ((AppCompatActivity) context).startActivityForResult(intent1, RequestEnum.VEDIO.getCode());
            File externalCacheDir = con.getExternalFilesDir(Environment.DIRECTORY_MOVIES);
/*            File file = new File("/data/user/0/com.example.myapplication/files/123");
            if (file.exists()) {
                if (!externalCacheDir.exists()) {
                    externalCacheDir.mkdirs();
                }
                file.renameTo(new File(externalCacheDir, "123.mp4"));
            }
            OkHttpClient client = new OkHttpClient();
            RequestBody muiltipartBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("username", "admin")//
                    .addFormDataPart("password", "admin")//
                    .addFormDataPart("file", "1.png", RequestBody.create(MediaType.parse("application/octet-stream"), file))
                    .build();

            Call call = client.newCall(new Request.Builder().url("http://192.168.1.111:8087").post(muiltipartBody).build());
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    return;
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    System.out.println(1);

                }
            });*/


//            return;
//
        });
        List<Button> buttonByIds = getButtonByIds(activity, GoEvent.getIdsOfHandles());
        for (Button buttonById : buttonByIds) {
            buttonById.setOnClickListener(new GoEvent());
        }
    }

    private static List<Button> getButtonByIds(AppCompatActivity activity, Set<Integer> ids) {
        List<Button> buttons = new ArrayList<>();
        if (ids == null) {
            return buttons;
        }
        for (Integer id : ids) {
            int i = id;
            buttons.add((activity.findViewById(id)));
        }
        return buttons;
    }

}
